<template>
  <div>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="small">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="账号" prop="username">
            <el-input v-model="form.username" placeholder="请输入账号" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="头像" prop="header">
            <ImageUpload @loadImageUrl="loadImageUrl" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="密码" prop="password">
            <el-input v-model="form.password" show-password placeholder="请输入密码" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="昵称" prop="nickName">
            <el-input v-model="form.nickName" placeholder="请输入昵称" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色">
            <el-select v-model="form.roleIds" style="width: 215px" multiple placeholder="请选择">
              <el-option
                v-for="item in roleOptions"
                :key="item.roleId"
                :label="item.roleName"
                :value="item.roleId"
              />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱" maxlength="50" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态">
            <el-radio-group v-model="form.status">
              <el-radio
                :key="1"
                :label="1"
              >启用
              </el-radio>
              <el-radio
                :key="0"
                :label="0"
              >禁用
              </el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="备注">
            <el-input v-model="form.note" :rows="2" type="textarea" placeholder="请输入备注" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="dialog-footer">
        <el-button v-if="!loading" type="primary" @click="add">提 交</el-button>
        <el-button v-else type="primary">提交中...</el-button>
        <el-button type="warning" @click="form = {status: 1}">重 置</el-button>
      </div>
    </el-form>

  </div>
</template>

<script>
import userApi from '@/api/system/sys-user'
import roleApi from '@/api/system/sys-role'

import md5 from 'js-md5'
import ImageUpload from '@/components/Upload/ImageUpload.vue'

export default {
  components: { ImageUpload },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 用户表单
      form: {
        status: 1
      },
      // 角色集合
      roleOptions: [],
      // 表单校验对象
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 5, message: '用户名最短5位', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 5, message: '密码最短5位', trigger: 'change' }
        ],
        nickName: [
          { required: true, message: '请输入昵称', trigger: 'blur' }
        ],
        email: [
          {
            type: 'email',
            message: '\'请输入正确的邮箱地址',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    // 添加
    add() {
      // 校验
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true
          // 1.密码要md5加密
          const form = { ...this.form }
          form.password = md5(form.password)
          // 2.表单校验
          userApi.save(form).then(res => {
            this.$message.success(res.msg)
            this.resetForm('form')
            // 关闭对话框，并刷新列表
            this.$emit('close')
            this.$emit('after')
            this.loading = false
          })
        }
      })
    },
    loadRoleList() {
      roleApi.getRoleAll().then(res => {
        this.roleOptions = res.data
      })
    },
    // 回显头像
    loadImageUrl(path) {
      this.form.header = path
    }
  }
}
</script>
